﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

using MySql.Data.MySqlClient;

namespace MakeIT.MiraPark.Negocios
{
    public static partial class ModelBO
    {

        public static void getVeiculoXAvarias( VeiculoVO veiculoVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT Placa, IdColisao ");
            query.AppendLine("FROM VeiculoXAvaria ");
            query.AppendLine("WHERE Placa = ?Placa ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoVO.placa);

            MySqlDataReader reader = null;
            try
            {
                cmd.Connection.Open();

                reader = cmd.ExecuteReader();

                veiculoVO.veiculoXAvariaVOs.Clear();

                while (reader.Read())
                {
                    VeiculoXAvariaVO veiculoXAvariaVO = VeiculoXAvariaVO.CreateVeiculoXAvariaVO();
                    veiculoXAvariaVO.placa = reader.GetString("Placa");
                    veiculoXAvariaVO.idColisao = reader.GetInt32("IdColisao");

                    veiculoVO.veiculoXAvariaVOs.Add(veiculoXAvariaVO);
                }
                reader.Close();
            }
            catch (Exception ex){ throw ex; }
            finally { cmd.Connection.Close(); }
        }

        public static void setVeiculoXAvarias(List<VeiculoXAvariaVO> veiculoXAvariaVOs)
        {
            foreach (VeiculoXAvariaVO veiculoXAvariaVO in veiculoXAvariaVOs)
            {
                setVeiculoXAvaria(veiculoXAvariaVO);
            }
        }
        public static void setVeiculoXAvaria(VeiculoXAvariaVO veiculoXAvariaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("UPDATE VeiculoXAvaria ");
            query.AppendLine("SET Placa=?Placa, IdColisao=?IdColisao ");
            query.AppendLine("WHERE Placa=?Placa and IdColisao=?IdColisao ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoXAvariaVO.placa);
            cmd.Parameters.AddWithValue("?IdColisao", veiculoXAvariaVO.idColisao);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void newVeiculoXAvarias(List<VeiculoXAvariaVO> veiculoXAvariaVOs)
        {
            foreach (VeiculoXAvariaVO veiculoXAvariaVO in veiculoXAvariaVOs)
            {
                newVeiculoXAvaria(veiculoXAvariaVO);
            }
        }
        public static long newVeiculoXAvaria(VeiculoXAvariaVO  veiculoXAvariaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("INSERT INTO VeiculoXAvaria ");
            query.AppendLine("SET Placa=?Placa, IdColisao=?IdColisao ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoXAvariaVO.placa);
            cmd.Parameters.AddWithValue("?IdColisao", veiculoXAvariaVO.idColisao);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
                return cmd.LastInsertedId;
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }

        public static void delVeiculoXAvarias(List<VeiculoXAvariaVO> veiculoXAvariaVOs)
        {
            foreach (VeiculoXAvariaVO veiculoXAvariaVO in veiculoXAvariaVOs)
            {
                delVeiculoXAvaria(veiculoXAvariaVO);
            }
        }
        public static void delVeiculoXAvaria(VeiculoXAvariaVO veiculoXAvariaVO)
        {
            StringBuilder query = new StringBuilder();

            query.AppendLine("DELETE FROM VeiculoXAvaria ");
            query.AppendLine("WHERE Placa=?Placa and IdColisao=?IdColisao ");

            MySqlCommand cmd = getConnection();
            cmd.CommandText = query.ToString();

            cmd.Parameters.AddWithValue("?Placa", veiculoXAvariaVO.placa);
            cmd.Parameters.AddWithValue("?IdColisao", veiculoXAvariaVO.idColisao);

            try
            {
                cmd.Connection.Open();

                cmd.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { cmd.Connection.Close(); }

        }
    }
}
